package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Stocklist;
import com.ruoyi.system.service.IStocklistService;
import com.ruoyi.system.service.impl.StockServiceImpl;
import com.ruoyi.system.util.StockTask;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Stock;
import com.ruoyi.system.service.IStockService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 股票数据Controller
 *
 * @author ruoyi
 * @date 2021-12-27
 */
@CrossOrigin
@Controller
@RequestMapping("/system/stock")
public class StockController extends BaseController {
    private String prefix = "system/stock";

    @Autowired
    private IStockService stockService;

    @Autowired
    private IStocklistService stocklistService;

    @Autowired
    private RedisTemplate redisTemplate;


    @Autowired
    private StockTask stockTask;

    @RequiresPermissions("system:stock:view")
    @GetMapping()
    public String stock() {
        return prefix + "/stock";
    }

    /**
     * 查询股票数据列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(Stock stock) {
        startPage();
        List<Stock> list = stockService.selectStockList(stock);
        return getDataTable(list);
    }

    /**
     * 新增股票数据
     */
    @GetMapping("/index")
    public String index(ModelMap mmap) {
        List<Stocklist> stocklists = stocklistService.selectStocklistList(new Stocklist());
        mmap.put("stocklists", stocklists);
        return prefix + "/index";
    }


    @RequestMapping("/dataList")
    @ResponseBody
    public List<List<String>> dataList(Stock stock) {
        List<List<String>> dataList = new ArrayList<>();
        List<Stock> stockList = redisTemplate.opsForList().range(stock.getSymbol().substring(2), 0, -1);
        stockList = stockList.stream().sorted(Comparator.comparing(Stock::getDate)).collect(Collectors.toList());
//        List<Stock> stockList = StockServiceImpl.stocksMap.get(stock.getSymbol()).stream().sorted(Comparator.comparing(Stock::getDate)).collect(Collectors.toList());
        for (Stock stock1 : stockList) {
            List<String> childList = new ArrayList<>();
            childList.add(DateUtils.dateTime(stock1.getDate()));
            childList.add(stock1.getOpen().toString());
            childList.add(stock1.getTrade().toString());
            childList.add(stock1.getHigh().toString());
            childList.add(stock1.getLow().toString());
            dataList.add(childList);
        }
        return dataList;
    }

    /**
     * 导出股票数据列表
     */
    @RequiresPermissions("system:stock:export")
    @Log(title = "股票数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Stock stock) {
        List<Stock> list = stockService.selectStockList(stock);
        ExcelUtil<Stock> util = new ExcelUtil<Stock>(Stock.class);
        return util.exportExcel(list, "股票数据数据");
    }

    /**
     * 新增股票数据
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存股票数据
     */
    @RequiresPermissions("system:stock:add")
    @Log(title = "股票数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Stock stock) {
        return toAjax(stockService.insertStock(stock));
    }

    /**
     * 修改股票数据
     */
    @RequiresPermissions("system:stock:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Stock stock = stockService.selectStockById(id);
        mmap.put("stock", stock);
        return prefix + "/edit";
    }

    /**
     * 修改保存股票数据
     */
    @RequiresPermissions("system:stock:edit")
    @Log(title = "股票数据", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Stock stock) {
        return toAjax(stockService.updateStock(stock));
    }

    /**
     * 删除股票数据
     */
    @RequiresPermissions("system:stock:remove")
    @Log(title = "股票数据", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(stockService.deleteStockByIds(ids));
    }
}
